﻿package com.tudou.skin.common.button
{
    import com.tudou.skin.common.events.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class SelectButton extends Sprite implements ISelect
    {
        protected var rollOverOutSprite:Sprite;
        protected var isSelect:Boolean = false;
        protected var extendClick:Boolean = false;
        protected var isEnable:Boolean = true;
        protected var unselectedObj:InteractiveObject;
        protected var selectedObj:InteractiveObject;
        protected var disableObj:DisplayObject;
        protected var iOverOut:IOverOut;

        public function SelectButton(param1:InteractiveObject, param2:InteractiveObject, param3:DisplayObject, param4:Point = null, param5:Point = null, param6:Point = null)
        {
            if (param1 == null)
            {
                param1 = new SimpleButton();
            }
            if (param2 == null)
            {
                param2 = new SimpleButton();
            }
            if (param3 == null)
            {
                param3 = new SimpleButton();
            }
            rollOverOutSprite = new Sprite();
            addChild(rollOverOutSprite);
            addState(param1, param4);
            addState(param2, param5);
            addState(param3, param6);
            this.selectedObj = param1;
            this.unselectedObj = param2;
            this.disableObj = param3;
            selected = false;
            return;
        }// end function

        public function get enable() : Boolean
        {
            return isEnable;
        }// end function

        public function set enable(param1:Boolean) : void
        {
            isEnable = param1;
            doIt();
            return;
        }// end function

        private function doIt() : void
        {
            doVisible();
            doListeners();
            return;
        }// end function

        private function buttonClickHlr(event:MouseEvent) : void
        {
            dispatchEvent(new SelectEvent(SelectEvent.CLICK, false, false, isSelect));
            return;
        }// end function

        public function get clickContainer() : Boolean
        {
            return extendClick;
        }// end function

        public function get selected() : Boolean
        {
            return isSelect;
        }// end function

        private function rollOutHlr(event:MouseEvent) : void
        {
            if (!isEnable)
            {
                return;
            }
            iOverOut.isOver = false;
            return;
        }// end function

        private function doVisible() : void
        {
            selectedObj.visible = isEnable && isSelect;
            unselectedObj.visible = isEnable && !isSelect;
            disableObj.visible = !isEnable;
            var _loc_1:* = isEnable;
            this.buttonMode = isEnable;
            this.useHandCursor = _loc_1;
            return;
        }// end function

        public function setBackground(param1:DisplayObject, param2:Number = 0, param3:Number = 0) : void
        {
            param1.x = param2;
            param1.y = param3;
            addChildAt(param1, 0);
            return;
        }// end function

        public function set clickContainer(param1:Boolean) : void
        {
            if (param1 == extendClick)
            {
                return;
            }
            extendClick = param1;
            doListeners();
            return;
        }// end function

        private function doListeners() : void
        {
            listenerIt(this, isEnable && extendClick);
            listenerIt(selectedObj, isEnable && !extendClick && isSelect);
            listenerIt(unselectedObj, isEnable && !extendClick && !isSelect);
            return;
        }// end function

        public function set selected(param1:Boolean) : void
        {
            isSelect = param1;
            doIt();
            return;
        }// end function

        protected function addState(param1:DisplayObject, param2:Point) : void
        {
            if (param2 == null)
            {
                param2 = new Point();
            }
            param1.x = param2.x;
            param1.y = param2.y;
            param1.visible = false;
            addChild(param1);
            return;
        }// end function

        public function removeRollOverOut() : void
        {
            if (iOverOut)
            {
                rollOverOutSprite.removeChild(iOverOut.getDisplay());
                this.removeEventListener(MouseEvent.ROLL_OVER, rollOverHlr);
                this.removeEventListener(MouseEvent.ROLL_OUT, rollOutHlr);
                iOverOut = null;
            }
            return;
        }// end function

        public function getSkin() : InteractiveObject
        {
            return this;
        }// end function

        public function addRollOverOut(param1:IOverOut) : void
        {
            if (param1 == null)
            {
                return;
            }
            removeRollOverOut();
            iOverOut = param1;
            rollOverOutSprite.addChild(iOverOut.getDisplay());
            this.addEventListener(MouseEvent.ROLL_OVER, rollOverHlr);
            this.addEventListener(MouseEvent.ROLL_OUT, rollOutHlr);
            return;
        }// end function

        private function rollOverHlr(event:MouseEvent) : void
        {
            if (!isEnable)
            {
                return;
            }
            iOverOut.isOver = true;
            return;
        }// end function

        private function listenerIt(param1:EventDispatcher, param2:Boolean) : void
        {
            listener(param1, param2, MouseEvent.CLICK, buttonClickHlr);
            return;
        }// end function

    }
}
